import time

from common.base import Base, get_driver
from selenium.webdriver.common.by import By


class ContentsPage(Base):
    def click_accessories(self):
        """选择商品品类"""
        self.click((By.LINK_TEXT, "配件"))

    def click_commodity(self):
        """选择商品"""
        self.click((By.XPATH, "//a[@href='goods.php?id=69']"))

    def change_count(self, number=1):
        """修改商品数量"""
        self.clear((By.ID, "number"))
        self.send_keys((By.ID, "number"), number)

    def click_buy(self):
        """加入购物车"""
        self.click((By.XPATH, "//img[@src='themes/default/images/buybtn1.png']"))


if __name__ == '__main__':
    # 创建driver
    driver = get_driver()
    driver.get("http://localhost:8080/ecshop/")
    driver.maximize_window()
    # 创建页面对象
    contents = ContentsPage(driver)
    contents.click_accessories()
    contents.click_commodity()
    time.sleep(1)
    contents.change_count(2)
    time.sleep(1)
    contents.click_buy()
    time.sleep(3)
    contents.quit(3)
